package com.util
{
	
	
	import flash.geom.Point;
			
		
	public class Geom {
		
		
		// converts degress into radians
		public static function degrees( radians:Number ):Number
		{
			return ( radians * 180 / Math.PI );
		}
		
		// converts radians into degrees
		public static function radians( degrees:Number ):Number {
		
			if ( degrees )
				return ( degrees * 180 / Math.PI );
			else
				return NaN;
		
		}

		// return angle between p1 and p2	
		public static function angle( p1:Point, p2:Point ):Number {
		
			var a:Number;
			var dy:Number = p2.y - p1.y;
			var dx:Number = p2.x - p1.x;
			
			a = Math.atan2( dy, dx );
			
			if ( a == 0 ) {
			// dx == 0 || dy == 0
				
				if ( dx == 0 && dy == 0 ) return NaN;
			
				else if ( dx == 0 )
					a = ( dy > 0 ? 1.5*Math.PI : 0.5*Math.PI );
					
				else if ( dy == 0 )
					a = ( dx > 0 ? 0 : Math.PI );
					
			} else 
				a = ( a < 0 ? 2*Math.PI + a : a );
				
			//trace( p2 + " > " + p1 + " dy=" + dy + " dx=" + dx + " a= " + a );
			return a;
		
		}
		
		// returns index in points nearest to toPoint
		public static function nearest( points:Array, toPoint:Point ):int {
		
			var current:Point;
			var closestIndex:int = 0;
			var minDistance:Number = Point.distance( points[ closestIndex ], toPoint );
			var distance:Number;
		
			for ( var i:int=1; i<points.length; i++ ) {
			
				current = points[ i ];
				distance = Point.distance( current, toPoint );
				if ( distance < minDistance ) {
				
					closestIndex = i;
					minDistance = distance;
				
				}
				
			}
			return closestIndex;
		
		}
		
		public static function pointFromAngle( startPoint:Point, angle:Number, radius:Number ):Point {
		
			return new Point();
		
		}
		
		
	}
}